package com.arcvideo.acsconnect.utils;

import android.content.Context;
import android.content.res.Configuration;
import android.util.Log;
import android.view.Display;
import android.view.Surface;
import android.view.SurfaceView;
import android.widget.RelativeLayout;

import com.arcvideo.commondef.ArcSize;

import java.math.BigDecimal;
import java.util.List;

/**
 * @author shenmengchao
 * @version 1.0.0
 * @date 2017/8/3
 * @description
 */

public class ScreenUtil {

    public static int getCameraOrientation(Display display) {
        int cameraOrientation = 0;
        if (display != null) {
            //getRotation() Add in android api level 8
            int rotate = display.getRotation();
            switch (rotate) {
                case Surface.ROTATION_0:
                    cameraOrientation = 0;
                    break;
                case Surface.ROTATION_90:
                    cameraOrientation = 90;
                    break;
                case Surface.ROTATION_180:
                    cameraOrientation = 180;
                    break;
                case Surface.ROTATION_270:
                    cameraOrientation = 270;
                    break;
            }
        }

        return cameraOrientation;
    }

    /**
     * get the current orientation of the window.
     */
    public static boolean isScreenLandScape(Context context) {
        Configuration mConfiguration = context.getResources().getConfiguration(); // 获取设置的配置信息
        int ori = mConfiguration.orientation; // 获取屏幕方向
        if (ori == mConfiguration.ORIENTATION_LANDSCAPE) {
            // 横屏
            return true;
        } else if (ori == mConfiguration.ORIENTATION_PORTRAIT) {
            // 竖屏
            return false;
        }
        return false;
    }

    public static void setFillInRect(SurfaceView surfaceView, int frameWidth, int frameHeight, int windowWidth, int windowHeight) {

        float aspect_ratio = (float) frameWidth / (float) frameHeight;

        if (aspect_ratio != 0.0) {
            frameWidth = Float.floatToIntBits((Float.intBitsToFloat(frameHeight) * aspect_ratio));
        }

		/* start */
        if (frameWidth != 0 && frameHeight != 0) {
            int estimateW, estimateH;
            if (windowWidth * frameHeight > windowHeight * frameWidth) {
                estimateW = windowHeight * frameWidth / frameHeight;
                estimateH = windowHeight;
                if (estimateW % 4 != 0)
                    estimateW -= estimateW % 4;
            } else {
                estimateW = windowWidth;
                estimateH = windowWidth * frameHeight / frameWidth;
                if (estimateH % 4 != 0)
                    estimateH -= estimateH % 4;
            }
            int xOffset = (windowWidth - estimateW) / 2;
            int yOffset = (windowHeight - estimateH) / 2;
            if (xOffset % 4 != 0)
                xOffset -= xOffset % 4;
            if (yOffset % 4 != 0)
                yOffset -= yOffset % 4;
            Log.i("", "setFillInRect: " + xOffset + ", " + yOffset + ", " + estimateW + "x" + estimateH);
            setSurfaceRect(surfaceView, xOffset, yOffset, estimateW, estimateH);
        } else {
            setSurfaceRect(surfaceView, 0, 0, windowWidth, windowHeight);
        }
    }

    public static void setSurfaceRect(SurfaceView surfaceView, int x, int y, int w, int h) {
        RelativeLayout.LayoutParams lp;
        if (w <= 1 && h <= 1) {
            lp = (RelativeLayout.LayoutParams) (surfaceView.getLayoutParams());
            lp.leftMargin = surfaceView.getWidth();
            lp.topMargin = surfaceView.getHeight();
            lp.width = 0;
            lp.height = 0;
            surfaceView.setLayoutParams(lp);
        } else {
            lp = (RelativeLayout.LayoutParams) (surfaceView.getLayoutParams());
            lp.leftMargin = x;
            lp.topMargin = y;
            lp.width = w;
            lp.height = h;
            surfaceView.setLayoutParams(lp);
        }
    }

    public static ArcSize getOptimalPreviewSize(List<ArcSize> sizes, int w, int h) {
        double targetRatio = (double) w / h;
        if (sizes == null) return null;
        ArcSize optimalSize = null;
        for (ArcSize size : sizes) {
            if (size.getWidth() == w && size.getHeight() == h) {
                optimalSize = size;
                break;
            }
        }

        if (optimalSize == null) {
            double minDiff = -1.0;
            BigDecimal bDecimalx = new BigDecimal(-1.0);
            BigDecimal minBigD = null;

            for (ArcSize size : sizes) {
                double ratio = (double) size.getWidth() / size.getHeight();
                minBigD = new BigDecimal(minDiff);
                if (minBigD.compareTo(bDecimalx) == 0) {
                    //相等--等于-1.0表示是第一次
                    minDiff = Math.abs(targetRatio - ratio);
                    optimalSize = size;
                } else {
                    double tmpMin = Math.abs(targetRatio - ratio);
                    BigDecimal tmpMinBigD = new BigDecimal(tmpMin);

                    int compResult = minBigD.compareTo(tmpMinBigD);
                    if (compResult == 0) {
                        //相等，则取分辨率大的
                        if (size.getWidth() > optimalSize.getHeight()) {
                            optimalSize = size;
                        }
                    } else if (compResult > 0) {
                        //minDiff > tmpMin
                        minDiff = tmpMin;
                        optimalSize = size;
                    } else {
                        //minDiff < tmpMin 不进行任何处理
                    }
                }
            }//end for
        }
        return optimalSize;
    }

    public static void setFillOutRect(SurfaceView surfaceView, int frameWidth, int frameHeight,
                                      int windowWidth, int windowHeight) {
        float aspect_ratio = (float) frameWidth / (float) frameHeight;

        if (aspect_ratio != 0.0) {
            frameWidth = Float.floatToIntBits((Float.intBitsToFloat(frameHeight) * aspect_ratio));
        }
        /* start */
        if (frameWidth != 0 && frameHeight != 0) {
            int estimateW, estimateH;
            if (windowWidth * frameHeight > windowHeight * frameWidth) {
                estimateW = windowWidth;
                estimateH = windowWidth * frameHeight / frameWidth;
                if (estimateH % 4 != 0)
                    estimateH -= estimateH % 4;
            } else {
                estimateW = windowHeight * frameWidth / frameHeight;
                estimateH = windowHeight;
                if (estimateW % 4 != 0)
                    estimateW -= estimateW % 4;
            }
            int xOffset = (windowWidth - estimateW) / 2;
            int yOffset = (windowHeight - estimateH) / 2;
            if (xOffset % 4 != 0)
                xOffset -= xOffset % 4;
            if (yOffset % 4 != 0)
                yOffset -= yOffset % 4;
            setSurfaceRect(surfaceView, xOffset, yOffset, estimateW, estimateH);
        } else {
            setSurfaceRect(surfaceView, 0, 0, windowWidth, windowHeight);
        }

    }
}
